<template>
	<div>
		<div class="box">
			<uni-forms
				ref="form"
				:model="plan"
				validate-trigger="bind"
				:label-position="state == 'write' ? 'top' : 'left'"
				label-width="auto">
				<FromItem
					label="选择车型"
					:required="true"
					:state="state"
					name="carModelName">
					<carType
						:label="plan.carModelName"
						@sure="sure"
						:validate="v"
						:state="state"
						:params="{
							quotationProductSchemeId: details.schemeId,
							businessType: 7,
						}"></carType>
				</FromItem>
				<FromItem
					label="台数"
					:required="true"
					:state="state"
					:rule="[
						{
							required: true,
							errorMessage: '请输入台数',
						},
						{
							format: 'number',
							errorMessage: '台数只能输入数字',
						},
						{
							minimum: 1,
							errorMessage: '台数最小为 1',
						},
					]"
					name="quantityRequired">
					<template #labelIcon v-if="details.customerTypeCode == 1">
						<div>
							<uni-icons
								@click="click_quantityRequired"
								type="help-filled"
								size="16"></uni-icons>
						</div>
					</template>
					<AInput
						:state="state"
						:max="details.customerTypeCode == 1 ? 1 : undefined"
						type="number"
						:min="1"
						:precision="0"
						v-model="plan.quantityRequired"></AInput>
				</FromItem>
				<FromItem label="车辆种类" :state="state" name="carTypeStr">
					{{ plan.carTypeStr || '-' }}
				</FromItem>
				<FromItem
					label="车辆类型"
					:state="state"
					name="carPowerTypeStr">
					{{ plan.carPowerTypeStr || '-' }}
				</FromItem>
				<FromItem label="品牌" :state="state" name="carBrandName">
					{{ plan.carBrandName || '-' }}
				</FromItem>
				<FromItem label="车系" :state="state" name="carSeriesName">
					{{ plan.carSeriesName || '-' }}
				</FromItem>
				<FromItem label="车辆分类" :state="state" name="carSeriesType">
					<ASelectUni
						:dict="{
							list: dict.type.car_classify,
						}"
						:required="
							config.businessSource == '1' &&
							details.rentType_raw == '1'
								? true
								: false
						"
						:state="state"
						@change="change_carSeriesType"
						v-model="plan.carSeriesType"></ASelectUni>
				</FromItem>
				<FromItem label="车型" :state="state" name="carModelName">
					{{ plan.carModelName || '-' }}
				</FromItem>
				<FromItem label="厂商" :state="state" name="carManufacturer">
					{{ plan.carManufacturer || '-' }}
				</FromItem>
				<FromItem label="座位" :state="state" name="carSeating">
					<div v-if="showSelectSeat">
						<ASelectUni
							:dict="{
								list: selectSeatList,
							}"
							:state="state"
							v-model="plan.carSeating"></ASelectUni>
					</div>
					<div v-else>
						{{ plan.carSeating || '-' }}
					</div>
				</FromItem>
				<FromItem label="排量" :state="state" name="carCc">
					{{ plan.carCc || '-' }}
				</FromItem>
				<FromItem
					label="厂商指导价"
					state="readonly"
					name="carGuidePrice">
					<AInput
						state="readonly"
						type="price"
						v-model="plan.carGuidePrice"></AInput>
				</FromItem>
				<FromItem
					label="车辆使用性质"
					:state="state"
					:required="true"
					name="operationType">
					<ASelectUni
						:dict="{
							list: dict.type.quota_operation_type,
						}"
						:state="state"
						v-model="plan.operationType"></ASelectUni>
				</FromItem>
				<FromItem
					label="外观颜色"
					:state="state"
					:required="true"
					name="appearanceColor">
					<AInput
						:state="state"
						v-model="plan.appearanceColor"></AInput>
				</FromItem>
				<FromItem
					label="内饰颜色"
					:state="state"
					name="interiorColor"
					:required="true">
					<AInput
						:state="state"
						v-model="plan.interiorColor"></AInput>
				</FromItem>
				<FromItem
					label="出库费"
					:state="state"
					name="warehouseOutCharge">
					<AInput
						:state="state"
						type="price"
						v-model="plan.warehouseOutCharge"></AInput>
				</FromItem>
				<FromItem label="物流费" :state="state" name="logisticFee">
					<AInput
						:state="state"
						type="price"
						v-model="plan.logisticFee"></AInput>
				</FromItem>
				<FromItem
					label="采购价"
					:state="state"
					name="purchasePrice"
					:required="true">
					<AInput
						:state="state"
						type="price"
						v-model="plan.purchasePrice"
						@change="change_purchasePrice"></AInput>
				</FromItem>
				<FromItem
					v-if="
						details.carType == '2' &&
						details.carSourceType == '1' &&
						details.rentType_raw == '1'
					"
					label="到期评估价"
					:state="state"
					name="finalEvaluationPrice">
					<AInput
						:state="state"
						type="price"
						v-model="plan.finalEvaluationPrice"
						@change="change_finalEvaluationPrice"></AInput>
				</FromItem>
				<FromItem
					label="GPS费用"
					:state="state_gpsFee"
					name="gpsFee"
					:required="true">
					<AInput
						:state="state_gpsFee"
						type="price"
						@change="change_gpsFee"
						v-model="plan.gpsFee"></AInput>
				</FromItem>
				<FromItem
					label="年检费（年）"
					state="readonly"
					name="annualCheckFee">
					<AInput
						state="readonly"
						type="price"
						v-model="plan.annualCheckFee"></AInput>
				</FromItem>
				<FromItem
					label="询价依据"
					:state="state"
					name="insuranceConsultAccessory">
					<ImageCommon
						v-model="plan.insuranceConsultAccessory"
						:disabled="state !== 'write'"
						:upType="2"
						:maxCount="1"></ImageCommon>
				</FromItem>
			</uni-forms>
		</div>
	</div>
</template>

<script>
	import * as app from '@/api/index.js';
	import * as apis from '@/api/quota/apply/api.js';
	import * as utils from '@/pages/quota/libs/utils.js';
	import FromItem from '../formItem/index.vue';
	import carType from '@/pages/quota/AForm/carType/index.vue';
	import Upload from '@/components/lr-upload.vue';
	export default {
		name: '',
		components: { FromItem, carType, Upload },
		dicts: ['quota_operation_type', 'fuel_type', 'car_classify'],
		props: {},
		inject: [
			'details_provide',
			'config_provide',
			'deptList_provide',
			'state_provide',
			'index_provide',
			'planDicts_provide',
			'fromtype_provide',
		],
		data() {
			return {};
		},
		computed: {
			details() {
				return this.details_provide();
			},
			config() {
				return this.config_provide();
			},
			deptList() {
				return this.deptList_provide();
			},
			planDicts() {
				return this.planDicts_provide();
			},
			state() {
				return this.state_provide();
			},
			fromtype() {
				return this.fromtype_provide();
			},
			index() {
				return this.index_provide();
			},
			currentInfoData() {
				return this.details.productList?.[this.index] ?? {};
			},
			plan() {
				return this.currentInfoData?.plan ?? {};
			},
			firstInfoData() {
				return this.details.productList?.[0] ?? {};
			},
			state_gpsFee() {
				return this.state == 'readonly'
					? 'readonly'
					: this.config?.quotationProductSchemeRule
							?.openGpsModification == 1
					? 'write'
					: 'readonly';
			},
			v() {
				if (!this.details?.schemeId) {
					return false;
				}
				return true;
			},
			showSelectSeat() {
				return /(\-|\/)/g.test(this.plan.carSeatingType);
			},
			selectSeatList() {
				if (!this.plan?.carSeatingType) {
					return [];
				}
				if (this.plan.carSeatingType.indexOf('/') !== -1) {
					return this.plan.carSeatingType?.split('/');
				}
				if (this.plan.carSeatingType.indexOf('-') !== -1) {
					let arr = this.plan.carSeatingType?.split('-');
					if (arr?.length < 2) return [];
					let start = arr?.[0] ?? 1;
					let end = arr?.[1];
					let seat = [];
					for (let index = start; index <= end; index++) {
						seat.push(index);
					}
					return seat;
				}
			},
		},
		watch: {},
		filters: {},
		created() {},
		mounted() {},
		methods: {
			sure(val) {
				this.setCarData(val.item);
				this.$emit('getRefittedList');
				this.$emit('initInputState');
				this.$emit('initCalcData');
			},
			setCarData(res = {}) {
				//车型id
				this._set('carModelId', res.id);
				//车型
				this._set('carModelName', res.modelName);
				//品牌
				this._set('carBrandName', res.brandName);
				//品牌id
				this._set('carBrandId', res.carBrandId);
				//车系
				this._set('carSeriesName', res.seriesName);
				//车系id
				this._set('carSeriesId', res.carSeriesId);
				// 座位数
				this._set('carSeatingType', res.seatNumber);
				if (/(\-|\/)/g.test(res.seatNumber)) {
					this._set('carSeating', undefined);
				} else {
					this._set('carSeating', res.seatNumber);
				}

				// 排量
				this._set('carCc', res.liter);
				// 车辆种类
				this._set(
					'carTypeStr',
					res.modelType == '1'
						? '乘用车'
						: res.modelType == '2'
						? '物流车'
						: undefined,
				);
				// 是否新能源
				this._set('isGreen', res.isGreen);
				// this.formData.carPowerTypeStr = res.fuelTypeName; // 车辆类型
				if (this.notEmpty(res.isGreen)) {
					this._set(
						'carPowerTypeStr',
						res.isGreen == 1 ? '新能源车' : '燃油车',
					);
				}
				// 厂商
				this._set('carManufacturer', res.makerName);
				let price = utils.toNumberRide(res.price, 10000);
				// 厂商指导价
				this._set(
					'carGuidePrice',
					Number.isFinite(price) // 厂商指导价
						? price
						: undefined,
				);

				if (
					this.currentInfoData?.substituteMaintenanceLevel !== '2' &&
					this.config.businessSource == '2' && //渠道
					this.details.carType == '1' && // 新车
					this.details.carSourceType !== '2' // 不为现有
				) {
					let item = this.planDicts.levelList?.find(e => {
						let startPrice = utils.toNumberRide(
							e?.startMsrp,
							10000,
						);
						let endPrice = utils.toNumberRide(e?.endMsrp, 10000);
						let startRes = false;
						let endRes = false;
						if (e?.includeStart == 0) {
							startRes = price > startPrice;
						} else if (e?.includeStart == 1) {
							startRes = price >= startPrice;
						} else {
							startRes = true;
						}
						if (e?.includeEnd == 0) {
							endRes = price < endPrice;
						} else if (e?.includeEnd == 1) {
							endRes = price <= endPrice;
						} else {
							endRes = true;
						}
						return startRes && endRes;
					});

					if (
						item &&
						!(
							this.config.businessSource == '1' &&
							this.details.rentType_raw == '2'
						)
					) {
						this.currentInfoData.substituteMaintenanceLevel = '1';
						this.currentInfoData.substituteMaintenanceLevelType =
							item.id;
					}
				}
				if (res.isGreen == 1) {
					this.$emit('setInsuranceCarvesselCurrentAmount');
				}
			},
			change_finalEvaluationPrice(val) {
				if (this.notEmpty(val)) {
					this.currentInfoData.planConf.finalEvaluationPrice_state =
						'changed';
				} else {
					this.currentInfoData.planConf.finalEvaluationPrice_state =
						'';
				}
			},
			_set(prop, data) {
				this.$set(this.plan, prop, data);
			},
			change_purchasePrice() {
				this.$emit('initInputState');
				this.$emit('initCalcData');
			},
			click_quantityRequired() {
				uni.showModal({
					title: '',
					content: '当前承租方为个人类型，只能申请一台车辆！',
					showCancel: false,
				});
			},
			change_carSeriesType({ val, item }) {
				let [key, value] = val?.split('_');
				this.plan.carSeriesTypeKey = key; // 分类
				this.plan.carSeriesTypeVal = value; // 系数
			},
			change_gpsFee(val) {
				if (this.notEmpty(val)) {
					this.currentInfoData.planConf.gpsFee_state = 'changed';
				} else {
					this.currentInfoData.planConf.gpsFee_state = '';
				}
			},
		},
		beforeDestroy() {
			if (this.notEmpty(this.details?.productList?.[this.index])) {
				this._resetFields({
					data: this.plan,
				});
			}
		},
	};
</script>

<style lang="scss" scoped>
	.box {
		// padding: 20rpx;
		padding-top: 20rpx;
		background-color: #fff;
		border-radius: 8rpx;
		.title {
			border-bottom: 1px solid #f5f5f5;
			padding-bottom: 10rpx;
			margin-bottom: 10rpx;
		}
	}
</style>
